package com.example.app.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.PixelFormat;
import android.graphics.SurfaceTexture;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.RequiresApi;
import android.view.Surface;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

import com.example.administrator.videoplay.R;
import com.example.app.manager.c2.Camera2Manager;
import com.example.app.manager.MediaPlayerManager;
import com.example.app.manager.MediaRecorderManager;
import com.example.app.view.c2.PreviewC2Layout;

import java.io.File;

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class Video5Activity extends Activity implements View.OnClickListener {

    private PreviewC2Layout previewLayout;
    //true 正在录制
    private boolean isTranscribe;
    //true 正在播放
    private boolean isPay;
    private Button videoBtn;
    private String tag = "VideoPayActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setWindow();
        setContentView(R.layout.activity_video_pay);
        previewLayout = (PreviewC2Layout) findViewById(R.id.preview_layout);
        videoBtn = (Button) findViewById(R.id.video_btn);
        videoBtn.setOnClickListener(this);
        findViewById(R.id.video_rest_tv).setOnClickListener(this);
        findViewById(R.id.picture_tv).setOnClickListener(this);
        findViewById(R.id.camera_switchover_tv).setOnClickListener(this);
        findViewById(R.id.camera_light_tv).setOnClickListener(this);
        findViewById(R.id.video_pay_tv).setOnClickListener(this);
        findViewById(R.id.camera_image_tv).setOnClickListener(this);


    }

    private String type;

    private void setWindow() {
        Intent it = getIntent();
        type = it.getStringExtra("arg0");
        //
        requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉标题栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);// 设置全屏
        if ("1".equals(type)) {
            // 设置横屏 90度
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }
        if ("2".equals(type)) {
            // 设置横屏 270度
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
        }
        if ("3".equals(type)) {
            // 设置竖屏显示 0度
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
        if ("4".equals(type)) {
            // 设置竖屏显示 180度
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
        }
        //选择支持半透明模式,在有surfaceview的activity中使用。
        getWindow().setFormat(PixelFormat.TRANSLUCENT);
    }

    @Override
    protected void onResume() {
        super.onResume();
        // 当屏幕关闭后重新打开, 若SurfaceTexture已经就绪,
        // 此时onSurfaceTextureAvailable不会被回调, 这种情况下
        // 如果SurfaceTexture已经就绪, 则直接打开相机,
        // 否则等待SurfaceTexture已经就绪的回调
        if (previewLayout.getPreviewView().isAvailable()) {
            Camera2Manager.getInstance().openBackgroundThread();
            Camera2Manager.getInstance().initCamera2(this);
        } else {
            previewLayout.getPreviewView().setSurfaceCallback();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        MediaRecorderManager.getInstance().setMediaWork(4);
        MediaPlayerManager.getInstance().setMediaWork(3);
        Camera2Manager.getInstance().onDestroy();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.camera_light_tv:
                //相机切换灯光
                if (isPay) {
                    MLog.e(tag, "无法相机切换灯光，正在播放视频");
                    break;
                }
                Camera2Manager.getInstance().setCameraLight();
                break;
            case R.id.camera_switchover_tv:
                //切换摄像头
                if (isPay) {
                    MLog.e(tag, "无法切换摄像头，正在播放视频");
                    break;
                }
                Camera2Manager.getInstance().setCameraSwitc();
                break;
            case R.id.video_rest_tv:
                //重新开始预览(停止播放 ,开启相机)
                MediaPlayerManager.getInstance().setMediaWork(3);
                Camera2Manager.getInstance().setRecoverPreview(this);
                previewLayout.setActivateTouch(false);
                break;
            case R.id.video_pay_tv:
                isPay = true;
                previewLayout.setActivateTouch(true);
                //关闭录制
                Camera2Manager.getInstance().onVideoStop();
                //播放视频
                MediaPlayerManager.getInstance().setDataSource("", getVoidPath());

                MediaPlayerManager.getInstance().setOnMediaListener(new MediaPlayerListener());
                SurfaceTexture surfaceTexture = previewLayout.getPreviewView()
                        .getSurfaceTexture();
                Surface surface = new Surface(surfaceTexture);
                MediaPlayerManager.getInstance().setSurfaceView(surface);
                MediaPlayerManager.getInstance().setMediaWork(0);

                break;
            case R.id.picture_tv:
                //拍照
                if (isPay) {
                    MLog.e(tag, "无法拍照，正在播放视频");
                    return;
                }
                Camera2Manager.getInstance().onCameraTakePicture(getImagePath());
                break;
            case R.id.video_btn:
                if (isPay) {
                    MLog.e(tag, "无法录制，正在播放视频");
                    return;
                }
                //录制视频
                isTranscribe = !isTranscribe;
                if (isTranscribe) {
                    //录制
                    Camera2Manager.getInstance().onVideoStart(this, getVoidPath());
                    MediaRecorderManager.getInstance().setMediaWork(1);
                    videoBtn.setText("录制中");
                } else {
                    //结束录制
                    MediaRecorderManager.getInstance().setMediaWork(4);
                    videoBtn.setText("录制停止");
                }
                break;
            case R.id.camera_image_tv:
                //查看图片
                Intent it = new Intent();
                it.setClass(this, ImageActivity.class);
                it.putExtra("arg0", type);
                it.putExtra("arg1", getImagePath());
                startActivity(it);
                break;
        }

    }

    protected String getVoidPath() {
        String videoPath = Environment
                .getExternalStorageDirectory().
                        getAbsolutePath() + File.separator
                + "videot";
        File file = new File(videoPath);
        if (!file.exists()) {
            file.mkdirs();
        }
        return videoPath + File.separator + "测试2.mp4";
    }

    protected String getImagePath() {
        String imagePath = Environment
                .getExternalStorageDirectory().
                        getAbsolutePath() + File.separator +
                "videot" + File.separator + "test.png";
        return imagePath;
    }

    //播放监听
    class MediaPlayerListener implements MediaPlayerManager.OnMediaPlayerListener {

        @Override
        public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
            previewLayout.getPreviewView().
                    setPayCenterCropFill(width, height);
        }


        @Override
        public void onBufferingUpdate(MediaPlayer mp, int percent) {

        }

        @Override
        public void onPayState(MediaPlayer mp, int state, String source) {
            MLog.e("--", "" + state);
            switch (state) {
                case 102:
                    //播放完成重播
                    MediaPlayerManager.getInstance().setMediaWork(2);
                    break;
            }
        }


        @Override
        public void onError(MediaPlayer mp, int what, int extra) {

        }
    }


    @Override
    protected void onDestroy() {
        MediaPlayerManager.getInstance().setMediaPlayerStop();
        super.onDestroy();
    }
}
